#include "database.h"
#include "func.h"

int connectMySQL(MYSQL* mysql){
    //从文件中读取出要连接数据库的参数
    //独处配置文件中的所有内容,配置文件的内容的顺序不能修改，并且不应该大于40960字节

    //打开配置文件
    int fd = open("../conf/mysql.conf",O_RDONLY);
    //读取内容
    char conf[40960] = {0};
    read(fd,conf,sizeof(conf));
    //关闭配置文件
    close(fd);

    //切分配置文件的内容
    char * argv[1024];
    int arglen = 0;
    argv[0] = strtok(conf," :\n");
    //printf("argv[0] = %s\n",argv[0]);
    if(argv[0]==NULL){
        printf("please check your mysql.conf!\n");
        return -1;
    }
    arglen++;
    for(int i=1;i<1024;i++){
        argv[i] = strtok(NULL," :\n");
        if(argv[i]==NULL){
            break;
        }
        ++arglen;
        //printf("argv[%d]= %s\n",i,argv[i]);
    }

    MYSQL* cret = mysql_real_connect(mysql,argv[1],argv[3],argv[5],argv[7],0,NULL,0);
    if(cret == NULL){
        printf("mysql_real_connect:%s\n",mysql_error(mysql));
        return -1;
    }

    return 0;
}

redisContext* connectRedis(){
    // 连接到本地Redis服务器，端口为6379
    redisContext* context = redisConnect("127.0.0.1", 6379);
    if (context == NULL || context->err) {
        if (context) {
            printf("Connection: %s\n", context->errstr);
            // 释放连接上下文
            redisFree(context);
        } else {
            printf("Error:memery allocate failed\n");
        }
        return NULL;
    }
    // 选择数据库1     
    redisReply* reply = (redisReply *)redisCommand(context, "SELECT 1");   
    if (reply == NULL) {        
        printf("SELECT: %s\n", context->errstr);   
        redisFree(context);  
        return NULL;  
    }   
    freeReplyObject(reply);
    return context;
}

int disconnectRedis(redisContext* context){
    redisFree(context);
    return 0;
}

int redisSet(redisContext* context,const char* key,const char* value){
    // 执行SET命令，设置一个键值对
    char command[2048] = {0};
    sprintf(command,"SET %s %s",key,value);
    /* printf("command = %s\n",command); */
    redisReply* reply = (redisReply*)redisCommand(context,command);
    if (reply == NULL) {        
        printf("SET: %s\n", context->errstr);   
        redisFree(context);  
        //重现建立一个新的连接
        context = connectRedis();
        return -1;  
    }   
    freeReplyObject(reply);
    return 0;
}

int redisGet(redisContext* context,const char* key,char * value){
    char command[2048] = {0};
    sprintf(command,"GET %s",key);
    /* printf("command = %s\n",command); */
    redisReply* reply = (redisReply*)redisCommand(context,command);
    if (reply == NULL) {        
        printf("GET: %s\n", context->errstr);   
        redisFree(context);  
        //重现建立一个新的连接
        context = connectRedis();
        return -1;  
    }   
    if (reply->type == REDIS_REPLY_STRING) {
        memset(value,0,1024);
        memcpy(value,reply->str,strlen(reply->str)+1);
        /* printf("获取的值为: %s\n", reply->str); */
    }
    freeReplyObject(reply);
    return 0;
}

int redisDel(redisContext* context,const char* key){
    char command[2048] = {0};
    sprintf(command,"DEL %s",key);
    printf("command = %s\n",command);
    redisReply* reply = (redisReply*)redisCommand(context,command);
    if (reply == NULL) {        
        printf("DEL: %s\n", context->errstr);   
        redisFree(context);  
        //重现建立一个新的连接
        context = connectRedis();
        return -1;  
    }   
    freeReplyObject(reply);
    return 0;
}
